<template>
  <Dialog title="审核" v-model="dialogVisible" @close="close">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="120px" v-loading="formLoading">
      <el-form-item label="审核信息" prop="infoName">
        {{ formData.infoName }}--{{ formData.infoTitle }} (售价：{{ formData.salePrice }})
      </el-form-item>
      <el-form-item label="信息状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态" class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.SUPPLY_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { SupplyInfoApi, SupplyInfoVO } from '@/api/wine/supplyinfo'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

import { handleTree } from '@/utils/tree'
/** 会员等级管理 表单 */
defineOptions({ name: 'Audit' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  userId: undefined,
  infoType: undefined,
  supplyTypeId: undefined,
  infoName: undefined,
  infoTitle: undefined,
  showYear: undefined,
  areaId: undefined,
  fragranceType: undefined,
  degree: undefined,
  capacity: undefined,
  packing: undefined,
  productArea: undefined,
  areaName: undefined,
  quantity: undefined,
  packages: undefined,
  labels: [],
  tags: [],
  image: '',
  images: [],
  unit: undefined,
  marketPrice: undefined,
  salePrice: undefined,
  description: undefined,
  tradeStatus: 1,
  isDuty: 1,
  viewCount: undefined,
  showCount: undefined,
  sort: 1,
  isTop: 1,
  isHot: 1,
  isRecommend: 1,
  status: undefined,
})
const formRules = reactive({
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref
let supplyList = ref() // 分类树
/** 打开弹窗 */
const open = async (row) => {
  dialogVisible.value = true
  resetForm()

  // 获得分类树
  const supply = await SupplyInfoApi.typeList({})
  supplyList.value = handleTree(supply)

  formData.value = row
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const close = () => {
  emit('success')
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SupplyInfoVO
    await SupplyInfoApi.updateSupplyInfo(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    infoType: undefined,
    supplyTypeId: undefined,
    infoName: undefined,
    infoTitle: undefined,
    showYear: undefined,
    areaId: undefined,
    fragranceType: undefined,
    degree: undefined,
    capacity: undefined,
    packing: undefined,
    productArea: undefined,
    areaName: undefined,
    quantity: undefined,
    packages: undefined,
    labels: [],
    tags: [],
    image: '',
    images: [],
    unit: undefined,
    marketPrice: undefined,
    salePrice: undefined,
    description: undefined,
    tradeStatus: 1,
    isDuty: 1,
    viewCount: undefined,
    showCount: undefined,
    sort: 1,
    isTop: 1,
    isHot: 1,
    isRecommend: 1,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>
